package com.itheima.service.serviceimpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.dao.MedicalExaminationPackageDao;
import com.itheima.pojo.Setmeal;
import com.itheima.service.MedicalExaminationPackageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class MedicalExaminationPackageServiceImpl implements MedicalExaminationPackageService {

    @Autowired
    private MedicalExaminationPackageDao medicalExaminationPackageDao;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("分页查询体检套餐");
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Setmeal> p = medicalExaminationPackageDao.findPage(queryPageBean);
        return new PageResult(p.getTotal(), p.getResult());
    }

    @Override
    public Setmeal findById(Integer id) {

        return medicalExaminationPackageDao.findById(id);
    }

    /*
     * 添加体检套餐
     */
    @Transactional
    public void addMedicalExaminationPackage(Setmeal setmeal, List<Integer> checkgroupIds) {
        medicalExaminationPackageDao.add(setmeal);
        Integer id = setmeal.getId();
        String url = setmeal.getImg();
        log.info("id:{}", id);
        log.info("checkgroupIds:{}", checkgroupIds);
        medicalExaminationPackageDao.addMedicalExaminationPackageAndCheckGroup(id, checkgroupIds);
        redisTemplate.opsForSet().add("setmealPicDbUrl2", url);
    }

    @Transactional
    public void editMedicalExaminationPackage(List<Integer> checkGroupIds, Setmeal setmeal) {
        //修改体检套餐数据
        medicalExaminationPackageDao.editMedicalExaminationPackage(setmeal);
        Integer id = setmeal.getId();
        //删除中间表数据
        medicalExaminationPackageDao.deleteMedicalExaminationPackageAndCheckGroup(id);
        //新增体检套餐与检查组的关联关系
        medicalExaminationPackageDao.addMedicalExaminationPackageAndCheckGroup(id, checkGroupIds);
    }
}
